1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*!
`isotope` lifetimes and instants
*/
use super::*;

mod constraint;
mod dependency;
mod instant;
mod linear;
mod matches;
mod relationship;
pub use constraint::*;
pub use dependency::*;
pub use instant::*;
pub use linear::*;
pub use matches::*;
pub use relationship::*;

/// The type of instants which satisfy a given constraint
#[derive(Clone, Eq, PartialEq)]
pub struct Lifetime {
    /// The constraint instants in this lifetime are under
    constraint: Constraint,
    /// The free variable set of this lifetime
    fv: SymbolSet,
    /// The code of this instant
    code: u64,
}

impl Lifetime {
    /// Create a new lifetime from a constraint
    pub fn new(constraint: Constraint) -> Lifetime {
        let fv = constraint.fv();
        let mut tmp = Lifetime {
            constraint,
            fv,
            code: 0,
        };
        tmp.fix_code();
        tmp
    }
    /// The unconstrained lifetime
    pub fn free() -> Lifetime {
        Self::new(Constraint::default())
    }
    /// Get the hasher used for lifetimes
    pub fn get_hasher() -> AHasher {
        AHasher::new_with_keys(34, 2123)
    }
    /// Fix the code of a constraint. Should be a no-op for user-facing code
    fn fix_code(&mut self) {
        let mut hasher = Self::get_hasher();
        self.constraint.hash(&mut hasher);
        self.code = hasher.finish()
    }
}

impl Debug for Lifetime {
    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
        write!(fmt, "Lifetime{:#?}", self.constraint)
    }
}

#[allow(clippy::derive_hash_xor_eq)]
impl Hash for Lifetime {
    #[inline]
    fn hash<H: Hasher>(&self, hasher: &mut H) {
        self.code().hash(hasher)
    }
}

impl Value for Lifetime {
    #[inline]
    fn is_ty(&self) -> bool {
        true
    }
    #[inline]
    fn is_groupoid(&self) -> bool {
        false
    }
    #[inline]
    fn ty(&self) -> &ValId {
        &*SET
    }
    #[inline]
    fn fv(&self) -> &SymbolSet {
        &self.fv
    }
    #[inline]
    fn is_lifetime(&self) -> bool {
        true
    }
    #[inline]
    fn into_enum(self) -> ValueEnum {
        ValueEnum::Lifetime(self)
    }
    #[inline]
    fn into_valid(self) -> ValId {
        //TODO: free instant...
        ValId::new_direct(ValueEnum::Lifetime(self))
    }
    #[inline]
    fn code(&self) -> u64 {
        self.code
    }
    fn contains(&self, value: &ValId) -> Result<Match, Error> {
        let _constraint = value.instant_constraint()?;
        unimplemented!()
    }
}

lazy_static! {
    /// The unconstrainted lifetime
    pub static ref FREE_LIFETIME: ValId = ValId::new_direct(ValueEnum::Lifetime(Lifetime::free()));
}